package org.zdd.bookstore.web.controller;

import com.github.pagehelper.Page;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.zdd.bookstore.common.pojo.BSResult;
import org.zdd.bookstore.common.utils.BSResultUtil;
import org.zdd.bookstore.model.dao.BookInfoMapper;
import org.zdd.bookstore.model.dao.CommentMapper;
import org.zdd.bookstore.model.entity.BookInfo;
import org.zdd.bookstore.model.entity.Comment;
import org.zdd.bookstore.model.entity.User;
import org.zdd.bookstore.model.service.ICommentService;
import org.zdd.bookstore.model.service.IUserService;

import javax.servlet.http.HttpSession;
import java.awt.print.Book;
import java.util.*;

@Controller
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private ICommentService commentService;

    /**
     * 添加评论
     * @return
     */
    @ResponseBody
    @PostMapping("/addComment")
    public String getComment(@RequestBody Comment comment) {

        if (commentService.addComment(comment) > 0) {
            return "SUCCESS";
        }else {
            return "FALSE";
        }
    }

    /**
     * 删除评论
     * @return
     */
    @GetMapping("/deleteComment/{commentId}")
    public String deleteComment(@PathVariable("commentId") Integer commentId) {
        //检查是否为管理员
        Integer i = commentService.selectIsAdmin(commentId);
        if (i!=1){
            return "exception";
        }
        //得到当前的bookId
        Integer bookId = commentService.selectBookId(commentId);
        //删除评论
        BSResult bsResult = commentService.deleteComment(commentId);

        if (bsResult.getCode() == 200) {
            return "redirect:/book/info/" + bookId;
        }
        return "exception";
    }
}
